from sanic import Sanic
from sanic.response import json,text

app = Sanic.get_app("myapp")


# @app.get("/")
@app.route('/test', methods=["POST", "PUT"])
async def hello_world(request):
    return text("Hello, world.")

#route
@app.route("/handtest")
async def handler(request):
    #9种返回类型
    return json({"foo": "bar"})


# app.add_route(handler, "/test")

@app.route("/path/to/<foo:str>")
async def handler(request, foo: str):
    ...

# @app.route("/path/to/<foo:ymd>")
# async def handler(request, foo: datetime.date):
#     ...
# /path/to/2021-03-28

# 基于处理程序方法名生成 url 的方法：app.url_for()
'''
@app.route('/')
async def index(request):
    # generate a URL for the endpoint `post_handler`
    url = app.url_for('post_handler', post_id=5)

    # Redirect to `/posts/5`
    return redirect(url)


@app.route('/posts/<post_id>')
async def post_handler(request, post_id):
    ...
'''
#特殊
'''
>> > app.url_for("post_handler", post_id=5, arg_one="one", _anchor="anchor")
'/posts/5?arg_one=one#anchor'

# _external requires you to pass an argument _server or set SERVER_NAME in app.config if not url will be same as no _external
>> > app.url_for("post_handler", post_id=5, arg_one="one", _external=True)
'//server/posts/5?arg_one=one'

# when specifying _scheme, _external must be True
>> > app.url_for("post_handler", post_id=5, arg_one="one", _scheme="http", _external=True)
'http://server/posts/5?arg_one=one'

# you can pass all special arguments at once
>> > app.url_for("post_handler", post_id=5, arg_one=["one", "two"], arg_two=2, _anchor="anchor", _scheme="http",
                 _external=True, _server="another_server:8888")
'http://another_server:8888/posts/5?arg_one=one&arg_one=two&arg_two=2#anchor'
'''
#静态文件
'''
app.static("/", "/path/to/index.html")
'''



